package com.ruoyi.cms.controller;/**
 * @author :
 * @date : 2024-07-31 09:15
 * @description :
 **/

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.ruoyi.cms.config.AlipayConfig;
import com.ruoyi.cms.config.Constants;
import com.ruoyi.cms.domain.WxpayRequest;
import com.ruoyi.cms.service.impl.OrderServiceImpl;
import com.ruoyi.cms.utils.QRCodeUtil;
import com.ruoyi.cms.utils.WxPayUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @description: TODO
 * @author: 杜晓南
 * @date: 2024/7/31 9:15
 * @version: 1.0
 */
@Controller
@RequestMapping("pay")
public class PayController {

    @Autowired
    private OrderServiceImpl orderService;


    @Autowired
    private WxPayUtils wxPayUtils;


    @GetMapping("wxpay")
    public  void wxpay(String id, HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, IOException, DocumentException {
        //根据ID查询出订单金额
//        Order order =orderService.findOrderById(id);
        //将订单信息发送给微信支付平台
        String url="https://api.mch.weixin.qq.com/pay/unifiedorder";
        //1、封装请求报文
        WxpayRequest wxPayRequest = new WxpayRequest();
        wxPayRequest.setAppid(Constants.APP_ID);
        wxPayRequest.setMch_id(Constants.MCH_ID);

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        wxPayRequest.setNonce_str(uuid);
        wxPayRequest.setBody("拼夕夕商城订单");
        wxPayRequest.setOut_trade_no("dxn"+id);
        wxPayRequest.setTotal_fee(1);

        String remoteAddr = request.getRemoteAddr();
        wxPayRequest.setSpbill_create_ip(remoteAddr);
        wxPayRequest.setNotify_url("http://wxpaydxn.free.idcfengye.com/pay/wxresult");

        wxPayRequest.setTrade_type("NATIVE");
        Map<String,Object> map = new HashMap<>();
        Class<? extends WxpayRequest> aClass = wxPayRequest.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            XStreamAlias annotation = field.getAnnotation(XStreamAlias.class);
            field.setAccessible(true);
            if (annotation==null){
                map.put(field.getName(),field.get(wxPayRequest));
            }else {
                map.put(annotation.value(),field.get(wxPayRequest));
            }
        }
        map.remove("sign");

        Set<String> keySet = map.keySet();

        List<String> keyList = new ArrayList(keySet);

        Collections.sort(keyList);


        String singStr="";
        for (String key : keyList) {
            singStr+=key+"="+map.get(key)+"&";
        }
        singStr+="key="+Constants.API_KEY;
        String sign = DigestUtils.md5Hex(singStr).toUpperCase();
        wxPayRequest.setSign(sign);

        XStream xStream = new XStream();
//		xStream.autodetectAnnotations(true);
        xStream.alias("xml", WxpayRequest.class);
        String requestXML = xStream.toXML(wxPayRequest);

        requestXML = requestXML.replaceAll("__", "_");
        System.out.println(requestXML);
        //2、发送报文并接收结果
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        StringEntity stringEntity = new StringEntity(requestXML,"UTF-8");
        httpPost.setEntity(stringEntity);

        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity responseEntity = httpResponse.getEntity();
        String responseXML = EntityUtils.toString(responseEntity,"UTF-8");
        System.out.println(responseXML);
        //3、解析报文
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new ByteArrayInputStream(responseXML.getBytes()));

        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements();

        String codeUrl="";

        for (Element element : elements){
            if (element.getName().equals("code_url")){
                codeUrl = element.getStringValue();
            }
        }
        System.out.println(codeUrl);
        //接受微信支付平台返回的信息

//      这句把以上代码都封装到里面了
//		String codeUrl = wxPayUtils.wxPay(id);
        //从返回的信息中取出codeUrl返回给前端
        QRCodeUtil.createQRCode(response,codeUrl);

    }

    @RequestMapping("/wxresult")
    @ResponseBody
    public String wxresult(HttpServletRequest request) throws IOException, DocumentException {
        System.out.println("收到支付结果。。。。");
        ServletInputStream inputStream = request.getInputStream();
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputStream);

        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements();
        Map map = new HashMap();
        for (Element element : elements) {
            map.put(element.getName(),element.getStringValue());
            System.out.println(element.getName()+"-------"+element.getStringValue());
        }
        //验签
        String sign= (String) map.remove("sign");

        //获取所有key并排序
        Set<String> keySet = map.keySet();

        List<String> keyList = new ArrayList(keySet);

        Collections.sort(keyList);


        String singStr="";
        for (String key : keyList) {
            singStr+=key+"="+map.get(key)+"&";
        }
        singStr+="key="+Constants.API_KEY;
        String sign2 = DigestUtils.md5Hex(singStr).toUpperCase();

        if(!sign2.equals(sign)){
            System.out.println("验签失败！");
            return "FAIL";
        }
        System.out.println("验签通过");

        //根据订单编号查询订单表，判断支付金额和订单金额是否
        //修改订单状态为已支付
        //扣减锁定库存

        return "SUCCESS";
    }



    @PostMapping("/alipay")
    @ResponseBody
    public String alipay(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = request.getParameter("WIDout_trade_no");
        //付款金额，必填
        String total_amount = request.getParameter("WIDtotal_amount");
        //订单名称，必填
        String subject = request.getParameter("WIDsubject");
        //商品描述，可空
        String body = request.getParameter("WIDbody");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String body1 = alipayClient.pageExecute(alipayRequest).getBody();
        //输出
        return body1;
    }



    @RequestMapping("alipayReturn")
    public String alipayReturn(HttpServletRequest request) throws AlipayApiException {
        System.out.println("收到同步回调结果");
        /* *
         * 功能：支付宝服务器同步通知页面
         * 日期：2017-03-30
         * 说明：
         * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
         * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。


         *************************页面功能说明*************************
         * 该页面仅做页面展示，业务逻辑处理请勿在该页面执行
         */

        //获取支付宝GET过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            params.put(name, valueStr);
        }
        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名

        //——请在这里编写您的程序（以下代码仅作参考）——
        if(signVerified) {
            //商户订单号
            String out_trade_no = request.getParameter("out_trade_no");

            //支付宝交易号
            String trade_no = request.getParameter("trade_no");

            //交易状态
            String trade_status = request.getParameter("trade_status");

            return "redirect:http://localhost/paysuccess.html";
        }else {
            return "验签失败!";
        }
        //——请在这里编写您的程序（以上代码仅作参考）——
    }

    @RequestMapping("alipayNotify")
    @ResponseBody
    public String alipayNotify(HttpServletRequest request) throws AlipayApiException {
        System.out.println("收到异步回调结果");
        /* *
         * 功能：支付宝服务器异步通知页面
         * 日期：2017-03-30
         * 说明：
         * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
         * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。


         *************************页面功能说明*************************
         * 创建该页面文件时，请留心该页面文件中无任何HTML代码及空格。
         * 该页面不能在本机电脑测试，请到服务器上做测试。请确保外部可以访问该页面。
         * 如果没有收到该页面返回的 success
         * 建议该页面只做支付成功的业务逻辑处理，退款的处理请以调用退款查询接口的结果为准。
         */

        //获取支付宝POST过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            params.put(name, valueStr);
        }

        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名

        //——请在这里编写您的程序（以下代码仅作参考）——

	/* 实际验证过程建议商户务必添加以下校验：
	1、需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号，
	2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额），
	3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）
	4、验证app_id是否为该商户本身。
	*/
        if(signVerified) {//验证成功
            //商户订单号
            String out_trade_no = request.getParameter("out_trade_no");

            //支付宝交易号
            String trade_no = request.getParameter("trade_no");

            //交易状态
            String trade_status = request.getParameter("trade_status");

            if(trade_status.equals("TRADE_FINISHED")){
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序

                //注意：
                //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
            }else if (trade_status.equals("TRADE_SUCCESS")){
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序

                //注意：
                //付款完成后，支付宝系统发送该交易状态通知
            }

            return "success";

        }else {//验证失败
            return "fail";

            //调试用，写文本函数记录程序运行情况是否正常
            //String sWord = AlipaySignature.getSignCheckContentV1(params);
            //AlipayConfig.logResult(sWord);
        }

        //——请在这里编写您的程序（以上代码仅作参考）——
    }

}
